package cvut.swe.semestralka.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.repository.RepositoryException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cvut.swe.semestralka.crawler.Configuration;
import cvut.swe.semestralka.crawler.Crawler;
import cvut.swe.semestralka.crawler.CrawlerException;
import cvut.swe.semestralka.sesame.SesameDao;
import cvut.swe.semestralka.sesame.Timetable;

@Controller
public class MISWEController {
	
	private final Logger logger = Logger.getLogger(MISWEController.class);
	
	@Autowired
	private Crawler crawler;
	
	@Autowired
	private Configuration configuration;
	
	@Autowired
	private SesameDao dao;

	@RequestMapping(value="/", method=RequestMethod.GET)
	public String indexPage(){
		return "index";
	}
	
	@RequestMapping(value="/crawle-form", method=RequestMethod.GET)
	public String crawlePage(Model model){
		model.addAttribute("setup", true);
		model.addAttribute(new CrawlerForm());
		return "crawle";
	}
	@RequestMapping(value="/crawle-form", method=RequestMethod.POST)
	public String crawle(Model model, @Valid CrawlerForm form, BindingResult res){
		
		model.addAttribute("setup", false);
		
		if(res.hasErrors()){
			model.addAttribute("isOK", false);
			return "crawle";
		}
		
		try {
			crawler.parse(configuration, form);
		} catch (CrawlerException e) {
			logger.error("error pri parsingu", e);
			model.addAttribute("isOK", false);
			return "crawle";
		}
		model.addAttribute("isOK", true);
		return "crawle";
	}
	
	@RequestMapping(value="search", method=RequestMethod.GET)
	public String searchForm(Model model){
		List<String> availableLinks = new ArrayList<String>();
		SearchForm form = new SearchForm();
		Calendar calendar = Calendar.getInstance();
		form.setHour(calendar.get(Calendar.HOUR_OF_DAY) );
		form.setMinute(calendar.get(Calendar.MINUTE));
		try{
			availableLinks = dao.getAvailableLinks();
		}catch(Exception ex){
			logger.error("chyba pri retrivingu", ex);
		}
		model.addAttribute(form);
		model.addAttribute("links", availableLinks);
		return "searchForm";
	}
	
	@RequestMapping(value="search", method=RequestMethod.POST)
	public String search(Model model, @Valid SearchForm form, BindingResult res) throws RepositoryException, MalformedQueryException, QueryEvaluationException{
		List<String> availableLinks = dao.getAvailableLinks();
		model.addAttribute("links", availableLinks);
		//dao.getLinksByStation(form.getFromStation());
		if(res.hasErrors()){
			List<String> availableStations = new ArrayList<String>();
			try{
				//availableStations = dao.getStationsByLink(form.getBusLine());
			}catch(Exception ex){
				logger.error("chyba pri retrivingu", ex);
			}
			return "searchForm";
		}
		
		if(!dao.isStationInLink(form.getBusLine(), form.getFromStation(), form.getLastStation())){
			res.rejectValue("fromStation", "error.isnotlinestation");
			return "searchForm";
		};
		
		//dao.getTimetableForLinkInStation(form.getFromStation(), form.getFromStation());
		Timetable tm = dao.findsDeparts(form);
		tm.setSmer(form.getLastStation());
		model.addAttribute("tm", tm);
//		model.addAttribute(form);
		return "searchForm";
	}
	
	
}
